
/*
    IR
*/
#ifndef IR_H
#define IR_H

#include <Arduino.h>
#include <IRremote.hpp>
#include "PinDefinitionsAndMore.h" // Define macros for input and output pin etc.
#include "component_l298n.h"

#define DECODE_NEC
#define IR_RECEIVE_PIN 7

// IR_Remoter: ZTE
// ir code of the car status
// motor
#define IR_CAR_FORWARD 0x47
#define IR_CAR_LEFT 0x48
#define IR_CAR_STOP 0x49
#define IR_CAR_RIGHT 0x4A
#define IR_CAR_BACKWARD 0x4B

#define IR_CAR_SPEED_UP 0X43   // <<
#define IR_CAR_SPEED_DOWN 0x46 // >>

int cur_ir_cmd;

void do_ir_cmd(int ir_cmd)
{
  Serial.println(cur_ir_cmd);
  switch (ir_cmd)
  {
  case IR_CAR_FORWARD:
    cur_car_cmd = CAR_FORWARD;
    break;
  case IR_CAR_LEFT:
    cur_car_cmd = CAR_LEFT;
    break;
  case IR_CAR_RIGHT:
    cur_car_cmd = CAR_RIGHT;
    break;
  case IR_CAR_BACKWARD:
    cur_car_cmd = CAR_BACKWARD;
    break;
  case IR_CAR_STOP:
    cur_car_cmd = CAR_STOP;
    break;
  case IR_CAR_SPEED_UP:
    cur_car_cmd = CAR_SPEED_UP;
    break;
  case IR_CAR_SPEED_DOWN:
    cur_car_cmd = CAR_SPEED_DOWN;
    break;
  default:
    break;
  } // switch
  Serial.print("car cmd: ");
  Serial.println(cur_car_cmd);
  car_action(cur_car_cmd);
};

void setup_ir()
{

  Serial.println(F("START " __FILE__ " from " __DATE__ "\r\nUsing library version " VERSION_IRREMOTE));
  IrReceiver.begin(IR_RECEIVE_PIN, DISABLE_LED_FEEDBACK);
  Serial.print(F("Ready to receive IR signals of protocols: "));
  printActiveIRProtocols(&Serial);
  Serial.println(F("at pin " STR(ACTION_RECEIVE_PIN)));
}

void loop_ir()
{
  // IrReceiver
  if (IrReceiver.decode())
  {

    IrReceiver.printIRResultShort(&Serial);
    IrReceiver.printIRSendUsage(&Serial);
    if (IrReceiver.decodedIRData.protocol == UNKNOWN)
    {
      Serial.println(F("Received noise or an unknown (or not yet enabled) protocol"));
      IrReceiver.printIRResultRawFormatted(&Serial, true);
    }
    Serial.println();
    if (IrReceiver.decodedIRData.protocol != UNKNOWN)
    {
      cur_ir_cmd = IrReceiver.decodedIRData.command;
      Serial.println(cur_ir_cmd);

      do_ir_cmd(cur_ir_cmd);
    };
    IrReceiver.resume(); // Enable receiving of the next value
  };
}
#endif /* IR_H */